//
//  AEHistoryPointsViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/15.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHistoryPointsViewController.h"
#import "AE_Solar-Swift.h"
#import "AEHistoryPointsViewModel.h"
#import "AEHistoryPointsTopView.h"
#import "AEHistoryPointsViewCell.h"

@interface AEHistoryPointsViewController ()

@property (nonatomic, strong) AEHistoryPointsViewModel *viewModel;

@property (nonatomic, strong) AEHistoryPointsTopView *topView;

@end

@implementation AEHistoryPointsViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [AEHistoryPointsViewModel new];
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithTopView];
    
//    self.baseTableView.hidden = YES;
    
    self.baseTableView.top = self.topView.bottom;
    self.baseTableView.height = kScreenH - NAVBAR_HEIGHT_STATUS - self.topView.height;
    self.baseTableView.backgroundColor = kWhiteColor;
    [self.baseTableView registerClass:[AEHistoryPointsViewCell class] forCellReuseIdentifier:[AEHistoryPointsViewCell className]];
    
    [self doRefreshData];
}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kAEHistoryPointsTopViewHeight);
        _topView = [[AEHistoryPointsTopView alloc] initWithFrame:frame];
        [self.view addSubview:_topView];
    }
    [self.topView updateWithViewData:self.viewModel];
}

- (void)initWithFooterView {
    [super initWithFooterView];
    
    CGRect frame = CGRectMake(0, 0, kScreenW, IPHONEX_BOTTOM_SPACE);
    UIView *footerView = [[UIView alloc] initWithFrame:frame];
    self.baseTableView.tableFooterView = footerView;
}

- (void)doRefreshData {
    [super doRefreshData];
    self.viewModel.pageId = 1;
    
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    @weakify(self);
    [self.viewModel requestWithHistoryScores:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self.baseTableView reloadData];
        }
        [self endAllRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAEHistoryPointsViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AEHistoryScoresModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    AEHistoryPointsViewCell *cell = [AEHistoryPointsViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    return cell;
}

@end
